﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.CardConfig
{
    public partial class FormCardAuthority : Form
    {
        private bool change = false;
        private string dev_Id;
        private string door_Id;
        private string monitorId;
        private string monitorId_Name;
        private bool auth_Card = true;
        private Array cardId = Array.CreateInstance(typeof(string), 50);  // 一门上限 50 卡
        private int cardCount = 0;

        public FormCardAuthority(string devId, string Id, string MonitorDev_Id, string monitorDevId_Name, bool authCard)
        {
            InitializeComponent();

            dev_Id = devId;
            door_Id = Id;
            monitorId = MonitorDev_Id;
            monitorId_Name = monitorDevId_Name;
            auth_Card = authCard;
        }

        private void FormCardAuthority_Load(object sender, EventArgs e)
        {
            if (auth_Card)
            {
                this.Text = "门卡授权";
            }
            else
            {
                this.Text = "门卡查询";
            }


            var queryDoorId =
                from DoorId in Monitor_DB.instance.db.tb_card_door
                where DoorId.Door_tb_ID == Convert.ToInt32(door_Id)
                join card in Monitor_DB.instance.db.tb_card on DoorId.Card_tb_ID equals card.ID
                select new { VAL1 = DoorId, VAL2 = card };


            DataTable dtCardDoor = new DataTable("CardDoor");

            dtCardDoor.Columns.AddRange(new DataColumn[]
                {
                new DataColumn("门号", Type.GetType("System.String")),
                new DataColumn("卡号", Type.GetType("System.String")),
                 }
             );

            foreach (var item in queryDoorId)
            {
                dtCardDoor.Rows.Add(dev_Id, item.VAL2.Card_Id);
                cardId.SetValue(item.VAL2.Card_Id, cardCount);
                cardCount++;
            }

            this.dataGridView1.DataSource = dtCardDoor;

            if (cardCount == 0)
            {
                // 门卡授权 则设send button为 false
                if (auth_Card)
                {
                    this.button_send.Visible = false;
                }
            }

            


        }

        private void button_send_Click(object sender, EventArgs e)
        {
            string card = "授权卡号：";
            for (int p = 0; p < cardCount; ++p)
            {
                card += cardId.GetValue(p) + "; ";
            }

            if (auth_Card)
            {
                

                if (ConfigCmd.configCommand.instance.DoorAuthCard(dev_Id, monitorId, cardId, cardCount))
                {
                    UserLog.adduseroperlog(User.UserID, User.Username, "门卡授权", "成功", monitorId_Name, dev_Id, card);
                    MessageBox.Show("已进入发送队列！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.Dispose();
                }
                else
                {
                    UserLog.adduseroperlog(User.UserID, User.Username, "门卡授权", "失败", monitorId_Name, dev_Id, card);
                    MessageBox.Show("已进入发送队列！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    MessageBox.Show("发送失败!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                ConfigCmd.configCommand.instance.QueryDoor_AuthCard(dev_Id, monitorId);
                MessageBox.Show("已进入发送队列！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UserLog.adduseroperlog(User.UserID, User.Username, "门卡查询", "成功", monitorId_Name, dev_Id, card);
            }
        }
    }
}
